package com.s.core.root.data;

import com.google.common.collect.Lists;
import com.s.core.root.Ad;
import com.s.core.root.D;
import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.List;

public class _Pic extends _BasePic {
    private List<_Point> pointList;
    private List<List<_PointAvg>> pointAvgLists;

    private static final int POINT_AVG_LIST_SIZE = 4;
    @Getter
    @Setter
    private int showIndex;

    public void nextShowIndex() {
        showIndex++;
        if (POINT_AVG_LIST_SIZE + 1 == showIndex) {
            showIndex = 0;
        }
    }

    public _Pic(_AllData allData, D d, int loadMax) {
        super(allData, d, loadMax);
    }

    private List<_PointAvg> init(List<D> dList, int avgCount) {
        List<_PointAvg> result = Lists.newArrayList();
        for (int recentIndex = dList.size() - 1; recentIndex >= 0; recentIndex--) {
            D d = dList.get(recentIndex);
            //total有误差,如昨天停牌今天复牌,pos1为0，posAvg5不为0
            int total = Ad.me().getDatePos1Total().get(d.date);
            result.add(new _PointAvg(this, d, total, recentIndex, avgCount));
        }
        return result;
    }

    public List<_Point> init(List<D> dList) {
        List<_Point> pointList = Lists.newArrayList();
        for (int recentIndex = dList.size() - 1; recentIndex >= 0; recentIndex--) {
            D d = dList.get(recentIndex);
            int total = Ad.me().getDatePos1Total().get(d.date);
            pointList.add(new _Point(this, d, total, recentIndex));
        }
        return pointList;
    }


    @Override
    public void init(List<D> dList, Object o) {
        pointList = init(dList);
        pointAvgLists = Arrays.asList(
                init(dList, 5),
                init(dList, 10),
                init(dList, 15),
                init(dList, 20)
        );
    }

    @Override
    public List<? extends _BasePoint> getPointList() {
        if (pointAvgLists.size() != POINT_AVG_LIST_SIZE) {
            throw new RuntimeException();
        }
        if (showIndex == 0) {
            return pointList;
        } else if (showIndex <= POINT_AVG_LIST_SIZE + 1) {
            return pointAvgLists.get(showIndex - 1);
        } else {
            throw new RuntimeException();
        }
    }
}
